package com.robot.mvvm.ui.work

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.robot.base.base.activity.GLBaseFragment
import com.robot.base.configs.AppConstants
import com.robot.base.util.arouter.ARouterUtil
import com.robot.mvvm.BR
import com.robot.mvvm.R
import com.robot.mvvm.databinding.AppFragmentRepairWorkOrderBinding
import com.robot.mvvm.ui.work.adapter.RepairWorkOrderAdapter
import com.robot.mvvm.vm.work.RepairWorkOrderFragmentViewModel

class RepairWorkOrderFragment :
    GLBaseFragment<AppFragmentRepairWorkOrderBinding, RepairWorkOrderFragmentViewModel>() {

    var type = ""//0-未处理，1-已派单，2-已驳回，3-进行中，4-已退回，5-已完成。可选，不传则查全部

    var mAdapter: RepairWorkOrderAdapter? = null

    companion object {
        fun newInstance(type: String): RepairWorkOrderFragment {
            val fragment = RepairWorkOrderFragment()
            val bundle = Bundle()
            bundle.putString(AppConstants.BundleKey.TYPE, type)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun initParam() {
        super.initParam()
        type = arguments?.getString(
            AppConstants.BundleKey.TYPE
        ) ?: ""
    }

    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_repair_work_order

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initAdapter()
        viewModel.state = type
        viewModel.refresh()
    }

    private fun initAdapter() {
        mAdapter = RepairWorkOrderAdapter().apply {
            setOnItemClickListener { adapter, view, position ->
                ARouterUtil.goToActivityWithBundle(AppConstants.Router.Main.A_REPAIR_WORK_ORDER_DETAIL,
                    Bundle().apply
                    {
                        putString(
                            AppConstants.BundleKey.QUESTION_ID,
                            mAdapter?.data?.get(position)?.questionId
                        )
                    })
            }
        }

        binding.mRv.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = mAdapter
        }
    }


    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.refreshEvent.observe(this, Observer {
            it ?: return@Observer
            binding.refreshLayout.finishRefresh()
            binding.refreshLayout.finishLoadMore()
            binding.refreshLayout.setEnableLoadMore(it != 1)
        })
        viewModel.uc.refreshDataEvent.observe(this, Observer {
            if (viewModel.pageNo == 1) {
                mAdapter?.data?.clear()
            }
            mAdapter?.addData(it)
        })
        binding.refreshLayout.setOnRefreshListener {
            viewModel.refresh()
        }
        binding.refreshLayout.setOnLoadMoreListener {
            viewModel.loadNext()
        }
    }
}